var page = 1;
var pageOld = 1;
var pageNum = 10;
var pageI = 1;
var pageL = 5;

var systemOpenCityFuntionUrl = "cityWeb/findAllListPageByWhere.json";
var systemOpenCitySubmitUrl = "cityWeb/insertCtiy.json";
var systemOpenCityDeleteUrl = "cityWeb/deleteOpenCityByPrimaryKey.json";
if(loginUserType != 1){
    systemOpenCityFuntionUrl = "cityWeb/findAllUserListPageByWhere.json";
    systemOpenCitySubmitUrl = "cityWeb/insertUserCtiy.json";
    systemOpenCityDeleteUrl = "cityWeb/deleteUserOpenCityByPrimaryKey.json";
}

var systemOpenCityFuntion = function () {
    $.ajax({
        url:systemOpenCityFuntionUrl,
        type:'POST',
        dataType:"json",
        data:{
            where:$("input[name='where']").val(),page:page,pageNum:pageNum,userId:loginUserId
        },
        success:function(result){
            var tem = "<tr>" +
                "<th width='5%'><label><input id='checkAll' type='checkbox' /></label></th>" +
                "<th>ID</th>" +
                "<th>省份</th>" +
                "<th>城市</th>" +
                "<th>区域</th>" +
                "</tr>";
            $.each( result.openCityList, function(index, content){
                tem += "<tr>" +
                    "<td><label><input type='checkbox' name='subBox' class='selectDiv' value='"+content.id+"'/></label></td>" +
                    "<td><span class='tdSpan'>"+content.id+"</span></td>" +
                    "<td><span class='tdSpan'>"+content.province+"</span></td>" +
                    "<td><span class='tdSpan'>"+content.city+"</span></td>" +
                    "<td><span class='tdSpan'>"+content.area+"</span></td>" +
                    "</tr>";
            });
            $("#systemOpenCityTable").empty();
            $("#systemOpenCityTable").append(tem);

            var pageSum = result.pageSum;
            var numSum = result.numSum;
            var temPage ="<a href='#' onclick='pageFunction("+page+","+page+",\"em\","+pageSum+");'><em>&lt;&lt;</em></a>";

            if(pageSum<=5){
                pageI = 1;
                pageL = pageSum;
            }else if(pageSum>5 && pageSum-pageI < 4 || pageSum-page < 4){
                pageI = pageSum-4;
                pageL = pageSum;
            }else {
                if(page >= 5){
                    if(page > pageOld){
                        if(page-pageI == 4){
                            pageI = page;
                            pageL = page+4;
                        }
                    }else if(page < pageOld){
                        if(pageL-page == 4){
                            pageI = page-4;
                            pageL = page;
                        }
                    }
                }else {
                    pageI = 1;
                    pageL = pageI+4;
                }
            }

            for(var i=pageI;i<=pageL;i++){
                if(page == i){
                    temPage += "<a class='cur' href='#' onclick='pageFunction("+i+","+page+",\"onc\");'>"+i+"</a>";
                }else {
                    temPage += "<a href='#' onclick='pageFunction("+i+","+page+",\"onc\");'>"+i+"</a>";
                }
            }

            temPage += "<a href='#' onclick='pageFunction("+page+","+page+",\"ep\","+pageSum+");'><em>&gt;&gt;</em></a>" +
                "<span>共"+numSum+"条/"+pageSum+"页</span>" +
                "<input class='pageTxt' name='pageValue' type='text' value='' />" +
                "<input class='pageBut' type='button' value='GO' onclick='pageFunction(0,"+page+",\"select\");'/>";

            $("#systemOpenCityPage").empty();
            $("#systemOpenCityPage").append(temPage);

            //获取所有复选框选中返回选中的值
            var checkboxValueFunction = function(){
                var str="";
                $('input[name="subBox"]:checked').each(function(index, content){
                    if(index == $('input[name="subBox"]:checked').size() - 1){
                        str+=$(this).val();
                    }else {
                        str+=$(this).val()+",";
                    }
                })
                return str;
            }

            //动态加class
            $(".page a,.orderTit a").click(function(){
                $(this).addClass("cur").siblings().removeClass("cur");
            });

            //新建
            $(".newEm").click(function(){
                $(".deskDiv").css({
                    display:"block",height:$(document).height()
                });
                $(".roleNew,.teamDiv").css({
                    top:($(window).height()-$(".roleNew,.teamDiv").height())/2+$(window).scrollTop()+"px",
                    display:"block"
                });
            });

            //表单提交
            $("#systemOpenCitySubmit").click(function(){
                alert($("#province  option:selected").text());
                $.ajax({
                    url:systemOpenCitySubmitUrl,
                    type:'POST',
                    dataType:"json",
                    data:{
                        province: $("#province  option:selected").text(),
                        city:$("#city  option:selected").text(),
                        area:$("#district  option:selected").text(),
                        userId:loginUserId
                    },
                    success:function(result){
                        if(result.isOK == 'OK'){
                            navPills('systemOpenCityHtml',2);
                        }else {
                            hintsPValueFucntion(result.msg);
                        }
                    },
                    error:function(){
                        hintsPValueFucntion("请求失败");
                    }
                });
            });

            //删除
            $(".deleteEm").click(function(){
                if($('input[name="subBox"]:checked').size() != 1){
                    hintsPValueFucntion("请选择一条记录操作");
                }else {
                    $(".deskDiv").css({
                        display:"block",height:$(document).height()
                    });
                    $(".roleDele").css({
                        top:($(window).height()-$(".roleDele").height())/2+$(window).scrollTop()+"px",
                        display:"block"
                    });
                }
            });

            //删除操作
            $("#systemOpenCityDelete").click(function(){
                $.ajax({
                    url:systemOpenCityDeleteUrl,
                    type:'GET',
                    dataType:"json",
                    data:{
                        opentCityId:checkboxValueFunction()
                    },
                    success:function(result){
                        if(result.isOK == 'OK'){
                            navPills('systemOpenCityHtml',2);
                        }else {
                            hintsPValueFucntion(result.msg);
                        }
                    },
                    error:function(){
                        hintsPValueFucntion("请求失败");
                    }
                });
            });

            //复选框
            $("table tr th label,table tr td label").click(function(){
                $(this).toggleClass("on");
            })

            //全选
            $("#checkAll").click(function(){
                if(this.checked){
                    $("td label").addClass("on");
                    $("td :checkbox[name='subBox']").prop("checked", true);
                }else{
                    $("td label").removeClass("on");
                    $("td :checkbox[name='subBox']").prop("checked", false);
                }
            });

            var dianjixuanzekuang = function() {
                if ($('.selectDiv').length == $('.selectDiv:checked').length) {
                    $('#checkAll').prop({
                        checked : true
                    });
                    $("th label").addClass("on");
                } else {
                    $('#checkAll').prop({
                        checked : false
                    });
                    $("th label").removeClass("on");
                }
            }
            $(".selectDiv").click(function(){
                dianjixuanzekuang();
            });

            var closeDivFunction = function (){
                $(".deskDiv").fadeOut();
                $(".roleDele").fadeOut();
                $(".roleNew").fadeOut();
                $(".hints").fadeOut();
                $(".authority").fadeOut();
                $(".modifyOrder").fadeOut();
                $(".hintsDiv").fadeOut();
                $(".teamDiv").fadeOut();
                $(".refundDiv").fadeOut();
                $(".addNote").fadeOut();
                $(".evaluation").fadeOut();
            }

            //关闭
            $(".cancel,.confirm").click(function(){
                closeDivFunction();
            });

            //友情提示
            var hintsPValueFucntion = function(v){
                $("#hintsPValue").html(v);
                $(".hints").css({
                    top:($(window).height()-$(".hints").height())/2+$(window).scrollTop()+"px",
                    display:"block"
                });
            }
        }
    });
}

var pageFunction = function(p,o,t,s){
    if(t == 'select'){
        if(parseInt($("input[name='pageValue']").val()) < 1){
            page = 1;
        }else {
            page = parseInt($("input[name='pageValue']").val());
        }
        pageI = page - 2;
        pageL = page + 2;
    }else if(t == 'em'){
        if(p == 1){
            page = p;
        }else {
            page = p - 1;
        }
        if(p==5 || pageL-p == 4){
            pageI = p-4;
            pageL = p;
        }
    }else if(t == 'ep'){
        if(p == s){
            page = p;
        }else {
            page = p + 1;
        }
        if(p==5 || p-pageI == 4){
            pageI = p;
            pageL = p+4;
        }
    }else {
        page = p ;
    }
    systemOpenCityFuntion();
    pageOld = o;
}

$(function () {
    systemOpenCityFuntion();
    _init_area();
});